﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Xml;

namespace CheckStiRpt
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            this.Close();
        }
        private void CheckRptXML(XmlDocument xmlDoc, ref bool isNested, ref string sMSG)
        {
            isNested = false;
            sMSG = string.Empty;
            //是否有DataBand
            XmlNodeList DataBandList = xmlDoc.SelectNodes("//Components/*[@type='DataBand']");
            //没有DataBand或者只有一个就不判断了
            if (DataBandList.Count < 2) return;
            //richTextBox1.AppendText(string.Format("共{0}个DataBand节点", DataBandList.Count) + "\r\n");
            string[,] arr = new string[DataBandList.Count, 3];  //[DataBandRef,DataSource,Master]

            for (int i = 0; i < DataBandList.Count; i++)
            {
                arr[i, 0] = DataBandList[i].Attributes["Ref"].Value.ToString();
                if (DataBandList[i].SelectSingleNode("DataSourceName") != null)
                    arr[i, 1] = DataBandList[i].SelectSingleNode("DataSourceName").InnerXml;
                else
                    arr[i, 1] = "<null datasource>." + i.ToString();  //没定义就随便给个值
                if (DataBandList[i].SelectSingleNode("MasterComponent") != null)
                    arr[i, 2] = DataBandList[i].SelectSingleNode("MasterComponent").Attributes["isRef"].Value.ToString();
                else
                    arr[i, 2] = "-1";
                //richTextBox1.AppendText(string.Format("节点Ref={0},DataSourceName={1},Parent.isRef={2}", arr[i, 0], arr[i, 1], arr[i, 2]) + "\r\n");
            }
            for (int i = 0; i < DataBandList.Count; i++)
            {
                if (arr[i, 2] != "-1")//有主的databand
                {
                    for (int j = 0; j < DataBandList.Count; j++)//是否有嵌套关系的DataBand
                    {
                        if (i != j) //跳过自己
                        {
                            if ((arr[i, 2] == arr[j, 0]) && (arr[i, 1] == arr[j, 1]))//嵌套关系的DataBand是否指向同一个数据源
                            {
                                //出现嵌套了
                                isNested = true;
                                sMSG = string.Format("[{0}] is nested uesed by:[{1}] and [{2}]", arr[j, 1],DataBandList[j].Name, DataBandList[i].Name);
                                return;
                            }
                        }
                    }
                }
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            //System.Xml.XmlDocument xmldoc = new System.Xml.XmlDocument();
            //System.IO.MemoryStream rptStream = new System.IO.MemoryStream();
            //report.Save(rptStream);
            //rptStream.Seek(0, 0);
            //xmldoc.Load(rptStream);
            //textBox1.Text = xmldoc.InnerXml;
            //rptStream.Close();
            //rptStream = null;
            //GC.Collect();

            openFileDialog1.Filter = "XML|*.XML|Report|*.MRT";
            if (openFileDialog1.ShowDialog() != DialogResult.OK) return;
            XmlDocument xmlDoc = new XmlDocument();
            xmlDoc.Load(openFileDialog1.FileName);
            richTextBox2.Text = xmlDoc.InnerXml;
            XmlDocument xmlDoc1 = new XmlDocument();
            xmlDoc1.InnerXml = richTextBox2.Text;
            xmlDoc1.Save("ttt.xml");
            //xmlDoc.Load("report.xml");
            bool bNested=false;
            string sMsg = string.Empty;
            CheckRptXML(xmlDoc, ref bNested, ref sMsg);
            if (bNested)
                richTextBox1.AppendText(sMsg+"\r\n");
        }
    }
}
